package cn.edu.sjtu.se.dslab.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.sjtu.se.dslab.dao.IProjectDao;
import cn.edu.sjtu.se.dslab.dao.IVariableDao;
import cn.edu.sjtu.se.dslab.project.Project;
import cn.edu.sjtu.se.dslab.project.Variable;
import cn.edu.sjtu.se.dslab.service.IVariableService;

@Service("variableService")
@Transactional
public class VariableServiceImpl implements IVariableService {

	@Resource(name = "variableDao")
	private IVariableDao variableDao;
	
	@Resource(name="projectDao")
	private IProjectDao projectDao;
	
	@Override
	public boolean addVariable(Variable variable, long projectId) {
		Project project = projectDao.queryById(projectId);
		if (project == null) throw new NullPointerException("Project id not found " + projectId);
		variable.setProject(project);
		variableDao.save(variable);
		return true;
	}

	@Override
	public Variable updateVariable(Variable variable) {
		return variableDao.update(variable);
	}

	@Override
	public Variable getVariableById(long id) {
		return variableDao.queryById(id);
	}

	@Override
	public List<Variable> getVariableList() {
		return variableDao.queryByList();
	}

	@Override
	public boolean deleteVariableById(long variableId) {
		Variable variable = variableDao.queryById(variableId);
		if (null != variable) {
			variableDao.delete(variable);
			return true;
		}
		return false;
	}

	@Override
	public List<Variable> getVariableByProject(long projectId) {
		return variableDao.queryByProject(projectId);
	}
	
	@Override
	public List<Variable> getValidByProject(long projectId){
		return variableDao.queryValidByProject(projectId);
	}
	
	@Override
	public Variable getVariableByProjectAndName(long projectId, String name){
		return variableDao.queryByProjectAndName(projectId, name);
	}

}

 

















